package com.foxhorse.androidpractice.activity.customizeview;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.view.View;
import android.widget.LinearLayout;

import com.foxhorse.androidpractice.R;
import com.foxhorse.androidpractice.activity.BaseActivity;

/**
 * Created by fanyang on 2017/8/2.
 * 主要用户绘制一些基础的图形
 */

public class CustomizeDrawView1Activity extends BaseActivity {
    private final static String TAG = "CustomizeDrawView1Activity";
    private LinearLayout ll;
    private Paint paint;
    private MyView myView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_customize_view_1);
        ll = (LinearLayout) findViewById(R.id.ll);
//        开启抗锯齿
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        myView = new MyView(this);
        ll.addView(myView);
    }

    class MyView extends View {
        public MyView(Context context) {
            super(context);
        }

        @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
//            绘制底色
            canvas.drawColor(Color.parseColor("#88880000"));
//            绘制圆形
            paint.setColor(Color.parseColor("#1234ab"));
            canvas.drawCircle(200, 130, 100, paint);
//            绘制描边的圆形
            paint.setColor(Color.parseColor("#4934ab"));
            paint.setStyle(Paint.Style.STROKE);
            paint.setStrokeWidth(10);
            canvas.drawCircle(500, 130, 100, paint);
            paint.setColor(Color.parseColor("#1234ab"));
            paint.setStyle(Paint.Style.FILL);
            canvas.drawCircle(500, 130, 90, paint);
//            绘制矩形实心
            paint.setColor(Color.parseColor("#493311"));
            paint.setStyle(Paint.Style.FILL);
            canvas.drawRect(100, 300, 300, 500, paint);
//                    绘制矩形空心
            paint.setColor(Color.parseColor("#bb3311"));
            paint.setStyle(Paint.Style.STROKE);
            paint.setStrokeWidth(10);
            canvas.drawRect(500, 300, 700, 500, paint);
//            绘制点（圆形和方形）
            paint.setStrokeWidth(20);
            paint.setStrokeCap(Paint.Cap.ROUND);
            canvas.drawPoint(100, 600, paint);
            paint.setStrokeWidth(20);
            paint.setStrokeCap(Paint.Cap.BUTT);
            canvas.drawPoint(200, 600, paint);
            paint.setStrokeWidth(20);
            paint.setStrokeCap(Paint.Cap.SQUARE);
            canvas.drawPoint(300, 600, paint);
//            批量绘制点
            paint.setColor(Color.parseColor("#b156aa"));
            paint.setStrokeCap(Paint.Cap.ROUND);
            float[] points = {0, 0, 50, 700, 100, 700, 150, 700, 200, 700, 250, 700, 300, 700};
            canvas.drawPoints(points, 2, 4, paint);
//            绘制实心椭圆和空心椭圆
            paint.setColor(Color.parseColor("#493311"));
            paint.setStyle(Paint.Style.FILL);
            canvas.drawOval(50, 750, 350, 900, paint);
            paint.setColor(Color.parseColor("#bb3311"));
            paint.setStyle(Paint.Style.STROKE);
            canvas.drawOval(400, 750, 700, 900, paint);
//            画线
            paint.setStrokeWidth(5);
            canvas.drawLine(100, 950, 700, 1000, paint);
//            批量画线
            paint.setColor(Color.parseColor("#67a193"));
            float[] lines = {0, 0, 0, 0,
                    100, 1000, 400, 1500,
                    100, 1500, 400, 1000,
                    100, 1600, 500, 1100,
                    100, 1700, 600, 1200,
            };
            canvas.drawLines(lines, 4, 16, paint);
        }
    }
}